package com.thundersoft.mm.phone;

import android.content.Context;

import com.example.yangxu0703.thundermapclient.iterface.ApkCallback;
import com.thundersoft.mm.FileTransfer;
import com.thundersoft.mm.phone.Wifi.WifiP2pSocket;
import com.thundersoft.mm.phone.Wifi.WifiP2pGroupOwnerManager;
import com.thundersoft.mm.phone.bt.BtTextServerListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;

public class ConnectionManager {
    public static final int TYPE_START_TAKEPHOTO = 0;
    public static final int TYPE_START_RECORDVIDEO = 1;
    public static final int TYPE_ENTER_DIRECTION = 2;
    public static final int TYPE_PHONE_RING = 3;
    public static final int TYPE_REQUEST_FILE = 4;
    public static final int TYPE_REQUEST_STOP_FILE = 5;//停止文件传输
    public static final int TYPE_PHONE_REJECT = 6;
    public static final int TYPE_PHONE_ACCEPTED = 7;
    public static final int TYPE_PHONE_IDLE = 8;
    public static final int TYPE_PHONE_OUT_CALL = 9;
    public static final int SEND_APK = 401;

    public static String mGlassesIp;
    public static final int TYPE_WIFIAP_INFO = 102;
    public static final int YTPE_DESTNATION = 101;
    public static final int TYPE_CONTACT = 100;
    public static Context mContext;
    public static String mWifiP2pName;
    public static final String MSG_START_P2P = "start-wifi-p2p";
    public static final String MSG_NAME_P2P = "name-wifi-p2p";
    public static final WifiP2pGroupOwnerManager wifiConnection = new WifiP2pGroupOwnerManager();
    public static BtTextServerListener btTextMsgListener;

    public static void init(Context context) {
        mContext = context;
        btTextMsgListener = new BtTextServerListener();
        btTextMsgListener.startListener();
    }

    public static void release() {
        btTextMsgListener.release(true);
    }


    public static void sendPhoneRinging(String number) {
        try {
            JSONObject o = new JSONObject();
            o.put("type", String.valueOf(TYPE_PHONE_RING));
            o.put("msg", number);
            ConnectionManager.btTextMsgListener.sendMsg(o.toString().getBytes());
        } catch (Exception e) {
        }
    }

    public static void sendDestination(String number) {
        try {
            JSONObject o = new JSONObject();
            o.put("type", String.valueOf(YTPE_DESTNATION));
            o.put("msg", number);
            ConnectionManager.btTextMsgListener.sendMsg(o.toString().getBytes());
        } catch (Exception e) {
        }
    }

    public static void sendPhoneNumber(ArrayList<String> contact) {
        try {
            StringBuffer sb = new StringBuffer();
            for (String info : contact) {
                if (info.equals(""))continue;
                sb.append(info.trim());
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", String.valueOf(TYPE_CONTACT));
            jsonObject.put("msg", sb.toString());
            ConnectionManager.btTextMsgListener.sendMsg(jsonObject.toString().getBytes());
        } catch (Exception e) {
        }
    }

    public static void sendPhoneIdel() {
        try {
            JSONObject o = new JSONObject();
            o.put("type", String.valueOf(TYPE_PHONE_IDLE));
            ConnectionManager.btTextMsgListener.sendMsg(o.toString().getBytes());
        } catch (Exception e) {
        }
    }

    public static void sendPhoneReject() {
        try {
            JSONObject o = new JSONObject();
            o.put("type", String.valueOf(TYPE_PHONE_REJECT));
            ConnectionManager.btTextMsgListener.sendMsg(o.toString().getBytes());
        } catch (Exception e) {
        }
    }

    public static void sendPhoneAccepted(String number) {
        try {
            JSONObject o = new JSONObject();
            o.put("type", String.valueOf(TYPE_PHONE_ACCEPTED));
            o.put("msg", number);
            ConnectionManager.btTextMsgListener.sendMsg(o.toString().getBytes());
        } catch (Exception e) {
        }
    }

    public static void sendDriveMessage(String number) {
        try {
            JSONObject o = new JSONObject();
            o.put("type", String.valueOf(TYPE_ENTER_DIRECTION));
            o.put("msg", number);
            ConnectionManager.btTextMsgListener.sendMsg(o.toString().getBytes());
        } catch (Exception e) {
        }
    }


    public static void sendPhoneOutCall(String number) {
        try {
            JSONObject o = new JSONObject();
            o.put("type", String.valueOf(TYPE_PHONE_OUT_CALL));
            o.put("msg", number);
            ConnectionManager.btTextMsgListener.sendMsg(o.toString().getBytes());
        } catch (Exception e) {
        }
    }


    public static void stopWifiP2p() {
        if (ConnectionManager.btTextMsgListener != null) {
            try {
                JSONObject o = new JSONObject();
                o.put("type", String.valueOf(5));
                ConnectionManager.btTextMsgListener.sendMsg(o.toString().getBytes());
            } catch (Exception e) {
            }
        }
    }

    public static void sendWifiInfo(String ssid, String pwd) {
        try {
            JSONObject o = new JSONObject();
            o.put("type", String.valueOf(TYPE_WIFIAP_INFO));
            o.put("msg", ssid + "=" + pwd);
            ConnectionManager.btTextMsgListener.sendMsg(o.toString().getBytes());
        } catch (Exception e) {
        }
    }

    public static boolean sendFile(File file, int type, ApkCallback apkCallback) {
        if (ConnectionManager.btTextMsgListener.connected && ConnectionManager.mGlassesIp != null) {
            JSONObject object = new JSONObject();
            try {
                object.put("type", String.valueOf(type));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            FileTransfer fileTransfer = new FileTransfer(file.getName(), file.length());
            fileTransfer.setHeader(object.toString());
            fileTransfer.setFilePath(file.getAbsolutePath());
            fileTransfer.setFileLength(file.length());
            WifiP2pSocket.transfer(fileTransfer, ConnectionManager.mGlassesIp,apkCallback);
        } else {
            return false;
        }
        return true;
    }


}
